<template>
    <!-- 导航栏 -->
    <van-nav-bar title="我的收货地址" left-text="返回" left-arrow @click-left="back" />

    <!-- 收货列表 -->
    <!-- 确认订单 -->
    <van-address-list
        v-if="action == 'order'"
        v-model="AddressDefault"
        :list="AddressList"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="onSelect"
    />

    <!-- 从会员中心 -->
    <van-address-list
        v-else
        v-model="AddressDefault"
        :list="AddressList"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="onCheck"
    />
</template>
<script>
    export default {
        emits: ['toggle'],
        created() {
            this.$emit('toggle', false);
            this.LoginUser = this.$cookies.get('LoginUser')
            this.action = this.$route.query.action ? this.$route.query.action: ''
            this.AddressData();
        },
        data() {
            return {
                AddressList: [],
                chosenAddressId: null,
                action: null,
                LoginUser: {},
                AddressDefault: null
            }
        },
        methods: {
            async AddressData() {
                let result = await this.$api.AddressIndex({ userid: this.LoginUser.id })

                if (result.code == '1') {

                    let data = result.data

                    var list = []

                    //先判断是否有收货地址
                    if (data.length <= 0) {
                        //没有收货地址
                        this.$notify({
                            message: '暂无收货地址',
                            duration: 1000,
                        })

                        this.$cookies.remove('address')

                        return false
                    }

                    for (var item of data) {
                        //找出默认收货地址
                        if (item.status == 1) {
                            this.AddressDefault = item.id
                        }

                        //组装收货地址列表
                        let address = {
                            id: item.id,
                            name: item.consignee,
                            tel: item.mobile,
                            isDefault: item.status == 1 ? true : false
                        }

                        //详细地址
                        var info = ''

                        if (item.province) {
                            info += `${item.provinces.name}`
                        }

                        if (item.city) {
                            info += `${item.citys.name}`
                        }

                        if (item.district) {
                            info += `${item.districts.name}`
                        }

                        if (item.address) {
                            info += `${item.address}`
                        }


                        address.address = info

                        list.push(address)

                    }

                    this.AddressList = list
                }
            },
            back() {
                this.$router.push('/user/base/index')
            },
            onAdd() {
                this.$router.push('/user/address/add')
            },
            onEdit(val) {
                this.$router.push({
                    path:'/user/address/edit',
                    query:{ 
                        id: val.id
                    }
                })
            },
            async onCheck(val) {

                let data = {
                    id: val.id,
                    userid: this.LoginUser.id
                }

                let result = await this.$api.AddressCheck(data);

                if (result.code == '1') {
                    this.$notify({
                        type: 'success',
                        message: result.msg,
                        duration: 1000,
                    })
                    this.AddressData();
                } else {
                    this.$notify({
                        type: 'danger',
                        message: result.msg,
                        duration: 1000
                    })
                }

            },
            onSelect(val) {
                let data = {
                    id: val.id,
                    mobile: val.tel,
                    consignee: val.name
                }
                this.$cookies.set('address',data);
                this.$router.go(-1)
            }
        }
    }
</script>